package com.hd.trans.utils

import java.text.DecimalFormat

/**
 * @Author hjw
 * @Date 2024/8/23 14:22
 * @Description
 */
object SizeUtil {

    fun formatFileSize(fileSize: Long): String {
        val df = DecimalFormat("#.00")
        val wrongSize = "0B"
        if (fileSize == 0L) {
            return wrongSize
        }
        val fileSizeString: String = if (fileSize < 1024) {
            df.format(fileSize.toDouble()) + "B"
        } else if (fileSize < 1048576) {
            df.format(fileSize.toDouble() / 1024) + "KB"
        } else if (fileSize < 1073741824) {
            df.format(fileSize.toDouble() / 1048576) + "MB"
        } else {
            df.format(fileSize.toDouble() / 1073741824) + "GB"
        }
        return fileSizeString
    }


    /**
     * 精确到秒
     * @param time 毫秒
     * @return
     */
    fun formatVideoTimeToSecond(time: Long): String {
        val hours = time / (1000 * 60 * 60)
        val minutes = (time - hours * 1000 * 60 * 60) / (1000 * 60)
        val second = (time - minutes * 1000 * 60) / 1000
        val millis = time - minutes * 1000 * 60 - second * 1000
        val sb = StringBuilder()
        if (hours < 10) {
            sb.append("0")
        }
        sb.append(hours)
        sb.append(":")
        if (minutes < 10) {
            sb.append("0")
        }
        sb.append(minutes)
        sb.append(":")
        if (second < 10) {
            sb.append("0")
        }
        sb.append(second)
        return sb.toString()
    }
}